package main

import (
	"fmt"
	"github.com/Shopify/sarama"
)

func main() {
	//生产者配置
	config := sarama.NewConfig()
	config.Producer.RequiredAcks = sarama.WaitForAll          //ACK确认，发送完数据需要lenader和follow确认
	config.Producer.Partitioner = sarama.NewRandomPartitioner //分区 新选出一个分区
	config.Producer.Return.Successes = true                   //确认 成功交付的消息将在success chan返回
	//消费封装
	msg := &sarama.ProducerMessage{}
	msg.Topic = "shopping"
	msg.Value = sarama.StringEncoder("this is test log1")
	//	链接kafka
	client, err := sarama.NewSyncProducer([]string{"192.168.47.136:9092"}, config)
	if err != nil {
		fmt.Println("producer closed ，err", err)
		return
	}
	defer client.Close()
	//	发送消息
	pid, offest, err := client.SendMessage(msg)
	if err != nil {
		fmt.Println("send message failed,err:", err)
		return
	}
	fmt.Printf("pid%v offset:%v\n", pid, offest)
}
